import {
  envid,
  usertermdataCollection,
} from '../config.js';

const app = getApp();
const openid = app.openid;

console.log(envid)
//云开发的初始化
wx.cloud.init({
  env: envid,
  traceUser: true
})
const usertermDoc = wx.cloud.database().collection(usertermdataCollection);

export const asyncGetUsertermData = async function (){
  return new Promise((resolve, reject) => {
    usertermDoc
    .where({
      _id: openid,
    })
    .get({
      success(res) {
        console.log("获取当前用户的usertermData成功", res)
        resolve(res.data);
      },
      fail(err) {
        reject(err);
      },
    })
  })
}

export const asyncAddUsertermData = async function (userterData) {
  return new Promise((resolve, reject) => {
    usertermDoc.add({
      data: {
        _id: app.globalData.openid,
        usertermData: userterData,
      },
      success(res) {
        console.log("新建当前用户的usertermData成功", res)
        resolve(res);
      },
      fail(err) {
        reject(err);
      },
    })
  })
};

export const asyncUpdateUsertermData = async function (usertermData) {
  return new Promise((resolve, reject) => {
    usertermDoc
      .where({
        _id: openid,
      })
      .update({
        data: {
          usertermData: usertermData,
        },
        success(res) {
          console.log("更新当前用户的usertermData成功", res)
          resolve(res);
        },
        fail(err) {
          reject(err);
        },
      })
  })
}

// export default {
//   asyncGetUsertermData,
//   asyncAddUsertermData,
//   asyncUpdateUsertermData,
// }